let options = {
  Accept: 'text/plain',
  method: 'GET'
}
// let url = 'https://notes.micheliafigo.xyz/str.ce'
let url = 'https://static.micheliafigo.xyz/books/1581347142049.jpeg'

// let fn = (options) => {

//   fetch(url, options).then(res => {
//     res.then(res)
//   })
// }

async function fn(url) {
  let response = await fetch(url)

  const reader = response.body.getReader()

  // console.log('reader', reader)

  // Step 2：获得总长度（length）
  const contentLength = +response.headers.get('Content-Length')
  // console.log('contentLength', contentLength)

  // step3: 读取数据
  let receivedLength = 0 // 接收的字节长度
  let chunks = [] //  接收到的二进制的数组（包括body）

  while (true) {
    const { done, value } = await reader.read()
    // console.log(`reader.read() value:${(value || []).slice(0, 5)}，done:${done}`)
    if (done) break

    chunks.push(value)
    receivedLength += value.length

    console.log(`Received ${receivedLength} of ${contentLength}`)
  }

  let chunkAll = new Uint8Array(contentLength)
  let position = 0
  for (chunk of chunks) {
    chunkAll.set(chunk, position)
    position += chunk.length
  }

  // console.log('chunkAll', chunk)

  let blob = new Blob(chunkAll)

  // file
  // let file = new FileReader()
  // file.readAsText(blob)
  // file.onload = () => {
  //   console.log(window.ba = file.result)
  // }

  // URL
  // console.log(window.a = URL.createObjectURL(blob))

  // Step 5：解码成字符串
  console.log((window.result = new TextDecoder('utf-8').decode(chunkAll)))
}

fn(url)
